function [dat bci] = bciTransformData(epoch,bci,mode)

nD=ndims(epoch);
if mode==0, % train mode, estimate parameter
    if nD==3,
        if bci.param.doNormalizeFeat,
            % normalize to same sum under curve each channel,each frequency
            refSum = sum(abs(epoch(1,1,:)));
            bci.normScale = refSum./sum(abs(epoch),3);
            epoch = epoch.*repmat(bci.normScale,[1,1,size(epoch,3)]);
        else
            bci.normScale = ones(size(epoch,1),size(epoch,2));
        end
        % reshape
        dat=reshape(permute(epoch,[3 1 2]),...
            [size(epoch,3) size(epoch,1)*size(epoch,2)]);
        bci.reshape.reSize=[size(epoch,1),size(epoch,2)];
    elseif nD==2,
        % reshape
        dat=epoch';
        bci.normScale=1;
        bci.reshape.reSize=[1,size(epoch,1)];
    else
        error('Number of dimensions not supported');
    end
    % zeromean and scale data
    bci.trainMean = mean(dat,1);
    dat = dat - repmat(bci.trainMean,size(dat,1),1);
    bci.trainScale = 10./max(abs(dat(:)));
    dat = dat.*bci.trainScale;
else % test mode, apply parameter
    if bci.param.CSPfilter>0,
        dat=bciCSPtransform(bci,epoch(bci.goodChan,:));
    else
        dat=epoch(bci.goodChan,bci.selectedFeat).*bci.normScale;
        dat=reshape(dat,[1 size(dat,1)*size(dat,2)]);
    end
    dat=dat-bci.trainMean;
    dat = dat.*bci.trainScale;
end